<?
    final class Principio_Model_TypeExtTemplateMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "TypeExtTemplate"; }


        public static function delete(Principio_Model_Type $model_type)
        {
            try
            {
                self::getDBTable()->delete(['type_id=?' => $model_type->getID()]);
                return true;
            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                throw new RuntimeException("Couldn't delete templates.");
            };
        }


        public static function update(Principio_Model_Type $model_type)
        {
            try
            {
                self::getDBTable()->getAdapter()->beginTransaction();

                foreach ($model_type->getTemplates() as $model_template)
                    self::getDBTable()->update(['template' => $model_template->getTemplate()], ['type_id=?' => $model_type->getID(), 'ext_number=?' => $model_template->getExtNumber()]);

                self::getDBTable()->getAdapter()->commit();

                return true;

            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollBack();

                return false;
            };
        }


        public static function insert(Principio_Model_Type $model_type)
        {
            try
            {
                for ($i = 0; $i < $model_type->getExts() + Principio_Model_Templatable::TEMPLATE_SHIFT; $i++)
                    self::getDBTable()->insert(['type_id' => $model_type->getID(), 'ext_number' => $i, 'template' => ""]);

                return true;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }



        public static function fetch(Principio_Model_Type $model_type)
        {
            try
            {
                $values = self::getDBTable()->fetchAll(self::getDBTable()->select()->where("type_id=?", $model_type->getID()));
                return Principio_Model_Template::makeCollection($values->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

        /*

        public static function search($string)
        {
            $search_fields = array(
                "template" => "Template"
            );

            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);

            foreach ($result as $res)
            {
                $type_info = Principio_Model_TypeMapper::get($res["type_id"]);
                $model_search = new Principio_Model_Search($string, "Spa types");
                $menu_data = array("menu_name" => "types", "level" => EXPAND_TYPES, "open_level" => 1);
                $model_search->setId($res["type_id"])->setResultName($type_info->getCaption())->setOpenItemFunctionName("Types")->setParentID(false)->setMenuData($menu_data);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["type_id"]] = $model_search;
            }
            return $search;


        }
        */
    }
?>